﻿<html DIR="LTR" xmlns:tool="http://www.microsoft.com/tooltip" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8" />
    <META NAME="save" CONTENT="history" />
    <title>Data Source Sample</title>
    
    <link rel="stylesheet" type="text/css" href="../local/Classic.css">
      
    </link>
    
    <script src="../local/script.js">
      
    </script><script src="../local/script_main.js">&amp;nbsp;</script>
  </head>
  <body>
    <!--Topic built:09/24/2007 09:01:38-->

    
    
    
    
    
    
    
    
    
    <div id="header">
      <table width="100%" id="topTable"><tr>
          <td align="left">
            <span id="headerBold">Data Source Sample</span>
          </td>
          <td align="right">
            
          </td>
        </tr></table>
      
      
      
    </div>
    <div id="mainSection">
      
        
        
		<font color="DarkGray"><!-- [This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]<br/>[Documentation built on $$TIMESTAMP$$]  --> </font> <p /> <p />
	
        <div id="introductionSection" class="section">
    <p>This Microsoft SQL Server 2005 JDBC Driver sample application demonstrates how to connect to a SQL Server database by using a data source object. It also demonstrates how to retrieve data from a SQL Server database by using a stored procedure.</p>
    <p>The code file for this sample is named connectDS.java, and it can be found in the following location:</p>
    <p>&lt;<i>installation directory</i>&gt;\sqljdbc_&lt;<i>version</i>&gt;\&lt;<i>language</i>&gt;\help\samples\connections</p>
  </div><h1 class="heading">Requirements</h1><div id="requirementsSection" class="section">
    <content xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <p xmlns="">To run this sample application, you must set the classpath to include the sqljdbc.jar file. If the classpath is missing an entry for sqljdbc.jar, the sample application will throw the common "Class not found" exception. You will also need access to the SQL Server 2000 AdventureWorks sample database. </p>
      <p xmlns="">For more information about how to set the classpath, see <a href="6faaf05b-8b70-4ed2-9b44-eee5897f1cd0.htm">Using the JDBC Driver</a>.</p>
    </content>
  </div><h1 class="heading">Example</h1><div id="codeExampleSection" class="section">
    <description xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <content>
        <p xmlns="">In the following example, the sample code sets various connection properties by using setter methods of the <a href="097434fd-2b74-411c-a5ed-eba04481dde5.htm">SQLServerDataSource</a> object, and then calls the <a href="a15ee185-f433-4915-9c75-5ebd90c758fd.htm">getConnection</a> method of the <b>SQLServerDataSource</b> object to return a <a href="937292a6-1525-423e-a2b2-a18fd34c2893.htm">SQLServerConnection</a> object.</p>
        <p xmlns="">Next, the sample code uses the <a href="265b024c-898a-4bd0-98b5-f06076717b9e.htm">prepareCall</a> method of the <b>SQLServerConnection</b> object to create a <a href="30710a63-c05d-47d9-9cf9-c087a1c76373.htm">SQLServerCallableStatement</a> object, and then the <a href="dfff3358-f391-4af6-84c1-b94c029f506d.htm">executeQuery</a> method is called to execute the stored procedure.</p>
        <p xmlns="">Finally, the sample uses the <a href="eaffcff1-286c-459f-83da-3150778480c9.htm">SQLServerResultSet</a> object returned from the <b>executeQuery</b> method to iterate through the results returned by the stored procedure.</p>
      </content>
    </description>
    <div class="sampleCode"><span codeLanguage="other"><pre>import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

public class connectDS {

   public static void main(String[] args) {

      // Declare the JDBC objects.
      Connection con = null;
      CallableStatement cstmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection. 
         SQLServerDataSource ds = new SQLServerDataSource();
         ds.setUser("UserName");
         ds.setPassword("*****");
         ds.setServerName("localhost");
         ds.setPortNumber(1433); 
         ds.setDatabaseName("AdventureWorks");
         con = ds.getConnection();

         // Execute a stored procedure that returns some data.
         cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");
         cstmt.setInt(1, 50);
         rs = cstmt.executeQuery();

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println("EMPLOYEE: " + rs.getString("LastName") + 
               ", " + rs.getString("FirstName"));
            System.out.println("MANAGER: " + rs.getString("ManagerLastName") + 
               ", " + rs.getString("ManagerFirstName"));
            System.out.println();
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
         System.exit(1);
      }
   }
}</pre></span></div>
  </div><span id="seeAlsoSpan"><h1 class="heading">See Also</h1></span><div id="seeAlsoSection" class="section" name="collapseableSection"><a href="ce43cc20-46a3-42ff-a3fb-75ad1ed10e08.htm">Connecting and Retrieving Data</a><br /><br /></div><!--[if gte IE 5]>
			<tool:tip element="seeAlsoToolTip" avoidmouse="false"/><tool:tip element="languageFilterToolTip" avoidmouse="false"/><tool:tip element="roleInfoSpan" avoidmouse="false"/>
		<![endif]-->
      <div id="footer" class="section">
        
		<hr />
		
		<span id="fb" class="feedbackcss">
			
			
		</span>
		
		<a href="9bad553b-9e70-4696-8499-2e35f772a1e0.htm">
			
			© 2007 Microsoft Corporation. All rights reserved.
		</a>
 	
	
      </div>
    </div>
  </body>
</html>